package com.auto.learning.activity;

import java.util.ArrayList;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;

import com.auto.learning.R;
import com.auto.learning.adapter.TabViewPagerAdapter;
import com.auto.learning.bean.BookTypeBean;
import com.auto.learning.bean.ResponeResult;
import com.auto.learning.event.BookNumChangeEvent;
import com.auto.learning.event.EventBus;
import com.auto.learning.event.Handle;
import com.auto.learning.event.LoginSuccessEvent;
import com.auto.learning.fragment.TopBookListFragment;
import com.auto.learning.net.HttpRequestHelper;
import com.auto.learning.service.MusicPlayManager;
import com.auto.learning.service.MyUserInfo;
import com.auto.learning.thread.pool.Pool;
import com.auto.learning.tools.CommonMethod;
import com.auto.learning.tools.TemporaryVariable;
import com.auto.learning.view.SlidingTabLayout;
import com.squareup.otto.Subscribe;
import com.squareup.otto.ThreadEnforcer;

/**
 * @author wangjianjun
 */
public class TopBookListActivity extends BaseActivity {
	
	private TopBookListFragment weekBookListFragment,monthBookListFragment;
	private ViewPager viewPager;
	private ArrayList<Fragment> fragmentList = new ArrayList<Fragment>();
	String type = "2";
	BookTypeBean bean;
	
	SlidingTabLayout sliding_tabs;
	
	boolean isShow = true;
	
	private String mTabTitle[] = new String[]{"最新上架", "小编推荐"};
	
	private Handle handle = new Handle(TopBookListActivity.this){
		public void handleMessage(android.os.Message msg) {
			switch (msg.what) {
				case HttpRequestHelper.PUBLIC_LOGIN_SUCCESS:
					CommonMethod.makeNotice(TopBookListActivity.this, "登录成功");
					ResponeResult login_result = (ResponeResult) msg.obj;
					if(login_result != null){
						EventBus.getEventBus("login_success_refreshUI").post(login_result);
					}
					break;
				case HttpRequestHelper.PUBLIC_LOGIN_FAIL:
					CommonMethod.makeNotice(TopBookListActivity.this, msg.obj.toString());
					break;
			}
		};
	};

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.top_booklist_activity);
		registerEvent();
		TemporaryVariable.activities.push(this);
	}
	
	
	@Subscribe
	public void onEventPlayStute(Boolean isPlaying){
		if (isPlaying) {
			animPlaying();
		}else {
			animPlayed();
		}
		weekBookListFragment.onEventPlayStute(isPlaying);
		monthBookListFragment.onEventPlayStute(isPlaying);
	}
	
	
	@Override
	protected void onDestroy() {
		TemporaryVariable.activities.removeElement(this);
		unRegisterEvent();
		super.onDestroy();
	}
	
	public void registerEvent(){
		EventBus.getEventBus("m_play_stute" , ThreadEnforcer.MAIN).register(this);
	}
	
	public void unRegisterEvent(){
		EventBus.getEventBus("m_play_stute" , ThreadEnforcer.MAIN).unregister(this);
	}
	
	
	private void initView(){
		Intent intent = getIntent();
		type = intent.getStringExtra("type");
		bean = (BookTypeBean) intent.getSerializableExtra("bean");
		if (bean != null) {
			setTitle(bean.getTypeName());
		}
		
		weekBookListFragment = new TopBookListFragment();
		weekBookListFragment.setType(2);
		weekBookListFragment.setBookType(bean.getTypeId());
		monthBookListFragment = new TopBookListFragment();
		monthBookListFragment.setType(1);
		monthBookListFragment.setBookType(bean.getTypeId());
		fragmentList.add(weekBookListFragment);
		fragmentList.add(monthBookListFragment);

		viewPager = (ViewPager) findViewById(R.id.activity_main_viewpager);
		
		TabViewPagerAdapter adapter = new TabViewPagerAdapter(getSupportFragmentManager(), this, fragmentList, mTabTitle);
		viewPager.setOffscreenPageLimit(0);
	    viewPager.setAdapter(adapter);
		
		sliding_tabs = (SlidingTabLayout) findViewById(R.id.sliding_tabs);
		sliding_tabs.setCustomTabView(R.layout.tab_text_1, R.id.tv_tab_content,R.color.main_green_color,R.color.main_grake_color);
		sliding_tabs.setDistributeEvenly(true);
		sliding_tabs.setViewPager(viewPager);
	}
	
	@Override
	protected void onStart() {
		super.onStart();
		if (isShow) {
			isShow = false;
			initView();
		}
		sliding_tabs.setSelectedIndicatorColors(getResources().getColor(R.color.black));
		sliding_tabs.setBackgroundColor(getResources().getColor(R.color.black));
	}
	
	
	
	@Override
	public void onActivityResult(int requestCode, int resultCode, final Intent data) {
		super.onActivityResult(requestCode, resultCode, data);
		if (requestCode == 100 && resultCode == 203 && data != null) {
			Pool.getPool().execute(new Runnable() {
				@Override
				public void run() {
					HttpRequestHelper.login2(TopBookListActivity.this,data.getStringExtra("userId"), data.getStringExtra("nickName"),
							data.getStringExtra("headImgUrl"), data.getBooleanExtra("isweixin",false), handle);
				}
			});
		}else if (requestCode == 100 && resultCode == 204) {
			EventBus.getEventBus("login_refreshUI").post(new LoginSuccessEvent());
		}
	}
	
}
